package crmdna.member;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.googlecode.objectify.ObjectifyFilter;

import crmdna.client.Client;
import crmdna.common.APIException;
import crmdna.common.APIResponse.Status;
import crmdna.common.contactdetail.ContactDetailProp;
import crmdna.group.Group;
import crmdna.group.Group.GroupProp;
import crmdna.member.MemberCore.MemberProp;
import crmdna.user.User;
import crmdna.user.UserCore;

public class MemberCoreTest {
	
	private final LocalServiceTestHelper datastoreHelper =
		        new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()
		        .setApplyAllHighRepJobPolicy());
	
	private final String client = "isha";
	private final String validUser = "valid@login.com";
	private GroupProp sgp;
	private GroupProp kl;
	private GroupProp sydney;
	
    @Before
    public void setUp() {
        datastoreHelper.setUp();
        ObjectifyFilter.complete();
        
        Client.create(client);
        sgp = Group.create(client, "Singapore", User.SUPER_USER);
        assertEquals(1, sgp.groupId);
        
        kl = Group.create(client, "Malaysia/KL", User.SUPER_USER);
        assertEquals(2, kl.groupId);
        
        sydney = Group.create(client, "Australia/Sydney", User.SUPER_USER);
        assertEquals(3, sydney.groupId);
        
        User.create(client, validUser, sgp.groupId, User.SUPER_USER);
        assertEquals(1, UserCore.get(client, validUser).toProp().userId);
    }

    @After
    public void tearDown() {       
    	ObjectifyFilter.complete();
        datastoreHelper.tearDown();
    }
       
    @Test
    public void populateContactDetailsTest() {
    	//only not null values should be copied
    	MemberEntity memberEntity = new MemberEntity();
    	memberEntity.firstName = "Sathyanarayanan";
    	memberEntity.email = "sathya.t@ishafoundation.org";
    	memberEntity.mobilePhone = "+6598361844";
    	memberEntity.homePhone = "+6565227020";
    	
    	ContactDetailProp contact = new ContactDetailProp();
    	contact.mobilePhone = "+6593232152";
    	
    	memberEntity = MemberCore.populateContactDetails(memberEntity, contact);
    	assertEquals("Sathyanarayanan", memberEntity.firstName);
    	assertEquals(null, memberEntity.lastName);
    	assertEquals("sathya.t@ishafoundation.org", memberEntity.email);
    	assertEquals("+6565227020", memberEntity.homePhone);
    	assertEquals("+6593232152", contact.mobilePhone); //new mobile number
    	assertEquals("Sathyanarayanan".toLowerCase(), memberEntity.name);
    	assertEquals("s", memberEntity.nameFirstChar);
    	
    	//quick search tags
    	assertTrue(memberEntity.qsTags.contains("sat"));
    	assertTrue(memberEntity.qsTags.contains("t@i"));
    	assertTrue(memberEntity.qsTags.contains("932"));
    	assertTrue(memberEntity.qsTags.contains("152"));
    	
    	//should not contain old number
    	assertTrue(! memberEntity.qsTags.contains("844"));
    }
    
   
    
    @Test
    public void safeAddToMemcacheTest() {
    	String email = "email@email.com";
    	
    	MemberCore.safeAddToMemcache(client, email); //no exception
    	
    	//adding again causes exception
    	try {
    		MemberCore.safeAddToMemcache(client, email);
    	} catch (APIException ex) {
			assertEquals(Status.ERROR_RESOURCE_ALREADY_EXISTS, ex.statusCode);
		}
    	
    	//adding again causes exception
    	try {
    		MemberCore.safeAddToMemcache(client, email);
    	} catch (APIException ex) {
			assertEquals(Status.ERROR_RESOURCE_ALREADY_EXISTS, ex.statusCode);
		}
    	
    	//can add for another client
    	String client2 = "client2";
    	Client.create(client2);
    	MemberCore.safeAddToMemcache(client2, email); //no exception
    	
    	//adding again causes exception
    	try {
    		MemberCore.safeAddToMemcache(client2, email);
    	} catch (APIException ex) {
			assertEquals(Status.ERROR_RESOURCE_ALREADY_EXISTS, ex.statusCode);
		}
    }
            
    @Test 
    public void safeGetTest() {
    	ContactDetailProp contact = new ContactDetailProp();
    	contact.email = "Sathya.t@IshaFoundation.org";
    	contact.mobilePhone = "+6598361844";
    	contact.homePhone = "+6565227030";
    	
    	MemberProp prop = MemberCore.create(client, sgp.groupId, contact, false);    	
    	
    	prop = MemberCore.safeGet(client, prop.memberId).toProp();
    	assertEquals("sathya.t@ishafoundation.org", prop.contact.email);
    	assertEquals("+6598361844", prop.contact.mobilePhone);
    	assertEquals("+6565227030", prop.contact.homePhone);
    }
    
    @Test
    public void populateDependantIndexFieldsTest() {    	
    	assertTrue(false);    	
    }
    
    @Test
    public void populateDependantFieldsWithoutDBQueryTest() {    	
    	assertTrue(false);    	
    }
}
